﻿
using HBase;
using System;
using System.Collections.Generic;

namespace hCommand {
    public class SimulationMoveTask :ZtTask {
        protected override bool init() {
            time = 300;
            return true;
        }

        public override bool excute() {
            List<Node> nodeList = MyCache.getList<Node>();
            foreach( Node n in nodeList ) {
                Estimate es = n.getCurrent();
                if( es != null ) {
                    moveNext(es);
                }
            }


    
            foreach( Node n in nodeList ) {

                if( !n.free() ) {
                    continue;
                }
                opNode(n);
            }

            log(nodeList);
            return true;
        }

       void  log(List<Node> nodeList) {

            int all = 0;
            foreach (Node n in nodeList) {
                all += n.timeAxis.Count;
            }
           // consoleOnly("节点数量" + all);

        }

        void opNode(Node n) {

            List<Estimate> axis = n.timeAxis;

            for( int i = 0;i < axis.Count;i++ ) {//
                var tmpEs = axis[i];
                if( tmpEs.isFirst() ) {//任务开始位置
                    n.arrived(tmpEs);
                    break;
                }
            }
        }

        void moveNext(Estimate es) {
            long finshTicks = es.getMoveNext().Ticks;

            if( finshTicks < DateTime.Now.Ticks ) {
                Node node = es.getNode();
                if( !node.isCurrent(es.getId()) ) {
                    return;
                }


                Estimate nes = es.getNextEstimate();
                if( nes != null ) {
                    Node n = nes.getNode();
                    if( !n.isCurrent(nes.getId()) ) {
                        n.arrived(nes);
                    }

                } else {
                    if( es.getNode().getId() != 1000 ) {
                        consoleOnly(InfoCode.consleOnly_Simulation_finsh, es.taskNo + "finshed ");
                        es.getNode().clearCurrent(es.getId());
                    }  
                  
                }

            }

        }









    }
}
